layui.define(function(exports){
    var $ = layui.$;

    function JWmessage(){
        this.render = 'messagebox';
        //关闭消息框事件
        this._onCloseMessageEvent = function(obj){
          $(obj).click(function(){
            var jobj = $(obj).parent().parent().parent().parent();
            jobj.slideUp(500,function(){
              jobj.remove();
            });
          });
        };
        this._onAutoCloseMessageEvent = function(obj){
          setTimeout(function(){
            var jobj = $(obj).parent().parent().parent().parent();
            jobj.slideUp(500,function(){
              jobj.remove();
            });
          },5000);
        };
      }
      JWmessage.prototype = {
          setRender : function(renderid){
            this.render = renderid;
          },
          draw : function(messagetype,messagetext){
            var $ = layui.$;
            //创建消息提示框
            var messageframe = document.createElement("div");
            var jmessageframe = $(messageframe);
            jmessageframe.addClass('index_messageframe');
            if(messagetype == "message"){
              jmessageframe.addClass('index_messageframe_message');
            }
            else if(messagetype == "warning"){
              jmessageframe.addClass('index_messageframe_warning');
            }
            else if(messagetype == "error"){
              jmessageframe.addClass('index_messageframe_error');
            }else{
              jmessageframe.addClass('index_messageframe_message');
            }
            //创建消息框的table和tr
            var messagetb = document.createElement("table");
            var messagetr = document.createElement("tr");
            //创建消息标题框
            var messagetdtile = document.createElement("td");
            var jmessagetdtile = $(messagetdtile);
            jmessagetdtile.addClass('index_messagetitle');
            //判断不同的消息类型给消息加不同的td
            if(messagetype == "message"){
              jmessagetdtile.html('消息:');
            }
            else if(messagetype == "warning"){
              jmessagetdtile.html('警告:');
            }
            else if(messagetype == "error"){
              jmessagetdtile.html('错误:');
            }else{
              jmessagetdtile.html('消息:');
            }
            //创建消息内容框
            var messagecontent = document.createElement("td");
            var jmessagecontent = $(messagecontent);
            jmessagecontent.addClass('index_messagecontent');
            jmessagecontent.html(messagetext);
            //创建关闭按钮td
            var messageicon = document.createElement("td");
            var jmessageicon = $(messageicon);
            jmessageicon.addClass('index_messageicon');
            //创建关闭按钮
            var messageicona = document.createElement("a");
            var jmessageicona = $(messageicona);
            jmessageicona.attr('href','javascript:void(0)');
            jmessageicona.html("X");
            messageicon.appendChild(messageicona);
            messagetr.appendChild(messagetdtile);
            messagetr.appendChild(messagecontent);
            messagetr.appendChild(messageicon);
            messagetb.appendChild(messagetr);
            messageframe.appendChild(messagetb);
            var renderobj = document.getElementById(this.render);
            renderobj.appendChild(messageframe);
            this._onCloseMessageEvent(messageicona);
            this._onAutoCloseMessageEvent(messageicona);
          }
      };

    //外部接口
    var jw_message = {
        new: function(){
            return new JWmessage()
        }
    }

    exports('jw_message', jw_message);
})